﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;

using ihongma.Common;
namespace ihongma.Job
{
     
    public class JobUisdc
    {
        ihongma.BLL.article add = new ihongma.BLL.article();
        JumonyParser parser = new JumonyParser();
        Random seed = new Random();
        public void url(int j, int category_id, string url)
        {
            for (int ii = 1; ii <= j; ii++)
            {
                string showhtml = HttpRequestHelper.GetPageSourceByUrl(url + ii.ToString(), "utf-8");
                var dataDocument = parser.Parse(showhtml);
                var items = dataDocument.Find(".post-title a").ToArray();
                var tempitems = dataDocument.Find(".entry-summary").ToArray();
                var thumbnai = dataDocument.Find(".archive-thumbnail").ToArray();
                for (int i = 0; i < items.Count(); i++)
                {
                    pageinfo(items[i].Attribute("href").Value(), tempitems[i].InnerText(), category_id, items[i].InnerText(), thumbnai[i].Attribute("src").Value());

                }
            }
        }
        public void pageinfo(string usrl, string temp, int category_id,string title,string img)
        {
            if (!add.ExistsTitle(title))
            {
                //StringBuilder sb = new StringBuilder();
                string showhtml = HttpRequestHelper.GetPageSourceByUrl(usrl, "utf-8");
                var dataDocument = parser.Parse(showhtml);
                var items = dataDocument.Find(".post-content");
                // items.Find(".similarity .wumii-hook .ad .post-copyright").Remove();
                items.Find(".entry-content div").Reverse().Take(6).Remove();
                items.Find(".entry-content p").Reverse().Take(2).Remove();
                items.Find("[alt='youshege']").Reverse().Take(1).Remove();
                // items.Find("blockquote").Reverse().Take(1).Remove();

              

                    ihongma.Model.article ar = new ihongma.Model.article()
                    {
                        add_time = DateTime.Parse(dataDocument.Find(".published").Single().InnerText() + " " + DateTime.Now.AddHours(-seed.Next(12)).ToLongTimeString()),
                        author = "colt",
                        category_id = category_id,
                        channel_id = 1,
                        zhaiyao = temp,
                        click = 100,
                        content = items.Find(".entry-content").Single().ToString().Replace("优设网", "鸿马网").Replace("uisdc.com", "ihongma.com"),
                        //dataDocument.Find("#artical_real p").ElementAt(0).InnerHtml() + "<p>" + temp + "</p>",

                        title = dataDocument.Find(".post-title").Single().InnerText(),
                        digg_act = 0,
                        digg_good = 0,
                        form = "鸿马",
                        img_url = img,
                        is_hot = 0,
                        is_lock = 0,
                        is_msg = 0,
                        is_red = 0,
                        is_slide = 0,
                        is_top = 0,
                        seo_description = temp,
                        seo_keywords = dataDocument.Find(".post-title").Single().InnerText(),
                        seo_title = dataDocument.Find(".post-title").Single().InnerText(),
                        sort_id = 99,
                        link_url = ""

                    };
                    add.Add(ar);
                
            }
        }
    }

}
